<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="价格组" prop="priceGroupId">
            <el-select v-model="dataForm.priceGroupId" @change="selectChange($event)">
              <el-option v-for="item in priceGroupOptions" :key="item.value" :value="item.value" :label="item.name"></el-option>
            </el-select>
            <!-- <el-input v-model="dataForm.priceGroupId" placeholder="价格组id"></el-input> -->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="税率" prop="taxRate">
            <el-input v-model="dataForm.taxRate"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"></el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="含税价格" prop="taxPrice">
            <el-input v-model="dataForm.taxPrice"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="无税价格" prop="notTaxPrice">
            <el-input v-model="dataForm.notTaxPrice"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="零售价格" prop="price">
            <el-input v-model="dataForm.price"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
    <goods-search v-if="goodsSearchVisible" ref="goodsSearch" @refreshForm="updateFormData"></goods-search>
  </el-dialog>
</template>

<script>
  import { goodsPriceGetObj, goodsPriceAddObj, goodsPricePutObj } from '@/api/eims/goods-price.js'
  import { priceGroupFetchList } from '@/api/eims/price-group.js'
  import GoodsSearch from '@/components/business/goods-search-single.vue'

  export default {
    data() {
      return {
				ischeck: false,
        visible: false,
        dataForm: {
          id: 0,
          goodsId: '',
          goodsName: '',
          priceGroupId: '',
          priceGroupName: '',
          taxRate: '',
          taxPrice: '',
          notTaxPrice: '',
          price: '',
          memo: '',
          status: '1'
        },
        dataRule: {
          goodsId: [{ required: true, message: '商品信息id不能为空', trigger: 'blur' }],
          priceGroupId: [{ required: true, message: '价格组id不能为空', trigger: 'blur' }],
          taxRate: [{ required: true, message: '税率不能为空', trigger: 'blur' }],
          taxPrice: [{ required: true, message: '含税价格不能为空', trigger: 'blur' }],
          notTaxPrice: [{ required: true, message: '不含税价格(含税价格-税率)不能为空', trigger: 'blur' }],
          price: [{ required: true, message: '零售价格不能为空', trigger: 'blur' }]
        },
        priceGroupOptions: [],
        goodsSearchVisible: false
      }
    },
    components: { GoodsSearch },
    methods: {
      init(data) {
        this.dataForm.goodsId = data.goodsId
        this.dataForm.id = data.id || 0
        this.visible = true
        this.getPriceGroupList()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            goodsPriceGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              goodsPricePutObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList',this.dataForm.goodsId)
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              goodsPriceAddObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList',this.dataForm.goodsId)
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      // 打开选择商品弹窗
      selectGoods () {
        this.goodsSearchVisible = true
        this.$nextTick(() => {
          this.$refs.goodsSearch.init()
        })
      },
      // 选择商品
      updateFormData (data) {
        this.dataForm.goodsId = data.id
        this.dataForm.goodsName = data.name
      },
      // 获取价格组列表
      getPriceGroupList () {
        priceGroupFetchList().then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data.records
            let list = []
            resultData.forEach(item => {
              list.push({value: item.id,name:item.groupName})
            })
            this.priceGroupOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      selectChange ($event) {
        this.priceGroupOptions.forEach(item => {
          if(item.value == $event){
            this.dataForm.priceGroupName = item.name
          }
        })
      }
    }
  }
</script>
